<?php

namespace app\admin\controller;

use app\admin\logic\Options as Options_logic;
use think\Db;

/**
 * Class Options 类别控制器
 * @package app\admin\controller
 * @author: Huang
 */
class Options extends Admin
{
    /**
     * 设置列表
     * @return mixed
     */
    public function index()
    {
//        halt(self::getCommonModel()->getDeparNameById(2));//发送者
        $Options_logic = new Options_logic();
        $Optionss = $Options_logic->getAllOptionss();
        $this->assign('Optionss', $Optionss);
        return $this->fetch();
    }

    /**
     * 添加类别
     * @return mixed
     */
    public function add()
    {
        $Options_logic = new Options_logic();
        if ($this->request->isPost()) {
            $data = input('post.');
            $result = $Options_logic->addOptions($data);
            if (false === $result['result']) {
                $this->error($result['msg']);
            } else {
                $this->success($result['msg']);
            }
        } else {
            $pid = $this->request->param('id');
            !empty($pid) ?: $pid = 0;
            $this->assign('pid', $pid);
            return $this->fetch();
        }
    }


    /**
     * 编辑类别
     * @return mixed
     */
    public function edit()
    {
        $Options_logic = new Options_logic();
        if ($this->request->isPost()) {
            $data = input('post.');
            $result = $Options_logic->editOptions($data);
            if ($result['result'] === true) {
                $this->success($result['msg'],'index');
            } else {
                $this->error($result['msg']);
            }
        } else {
            $id = input('id');
            $Options = Db::name('Options')->where('id', $id)->find();
            $Options['option_value'] = json_decode($Options['option_value'],true);
            $this->assign('info', $Options);
            return $this->fetch();
        }
    }

    /**
     * 删除类别
     */
    public function del()
    {
        $id = $this->request->param('id', 0);
        $Options_logic = new Options_logic();
        $result = $Options_logic->delOptions($id);
        if ($result['result'] === true) {
            $this->success($result['msg']);
        } else {
            $this->error($result['msg']);
        }
    }

    /**
     * 设置类别默认基础角色
     * @return mixed
     * @author: Huang
     */
    protected function setstates()
    {
        if ($this->request->isPost()) {
            $authdata = $this->request->post();
            if (empty($authdata['id'])) {
                $this->error('参数丢失');
            } else {
                $data['id'] = $authdata['id'];
                $data['rule'] = implode(',', $this->request->post('rule/a'));
                $res = model('Options')->allowField(true)->isUpdate()->save($data);
                if ($res) {
                    $this->success('操作成功');
                } else {
                    $this->error(model('Options')->getError());
                }
            }
        } else {
            $Optionsid = input('id', 0, 'int');
            $Options = Db::name('Options')->where(['id' => $Optionsid])->find();//当前类别
            $auth_group = Db::name('laiyuan')->cache(6000)->select();//获取所有的角色
            $this->assign('Options', $Options);
            $this->assign('laiyuan', $auth_group);
            return $this->fetch();
        }
    }

}
